C Language Basic Syntax
C language specifies certain rules like how the code should start, how the code should end, when to use double quotes and when to use curly brackets etc.
A smallest individual unit of a C program is known as C Token. The tokens can either be a keyword, identifiers, variables or constant.
A C program can also be called as a collection of tokens.
Example
printf("Welcome to C Language");
Here the individual tokens are printf
, (
, "Welcome to C Language"
, )
, ;
.
Semicolons
Semicolon ;
is used to denote the end of a statement. Each statement should end with a semicolon.
If the statement does not end with semicolon then the compiler will think that the statement has not yet completed and would combine it with the next statement and lead to compilation error, which is otherwise called as syntax error.
Example 1
#include<stdio.h>
int main()
{
printf("Welcome to C Language");
return 0;
}
Output
Welcome to C Language
Example 2
#include<stdio.h>
int main()
{
printf("Welcome to C Language");
return 0;
}
This will lead to compilation error since the semicolon is missing at the end of printf()
function.
Comments
Comments are the plain text that are ignored by the compiler from compilation. Its mainly written for better understanding of the program. Comments are not compulsory, but it makes the program more descriptive and readable.
There are 2 ways to write comments
- Single line comment denoted by
//
symbol. - Multi-line comments which starts with
/*
and ends with*/
.
Example for comments
#include<stdio.h>
void main()
{
int a,b;
// To hold the sum of a & b
int result;
// Perform sum of a & b.
result = a + b;
/*
Print the value in result
*/
printf("Result is %d", result);
}
Syntax rules for C program
- All C statements should end with a semicolon.
- C language is a case sensitive in nature. All the instructions must use lower case letters.
- Whitespace is required between keyword and an identifier.